<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script th:src="@{/main/layui/layui.js}" charset="utf-8"></script>
    <script th:src="@{/main/js/jquery-2.1.3.min.js}" charset="utf-8"></script>
    <script th:src="@{/main/js/day.js}" charset="utf-8"></script>
    <link rel="stylesheet" th:href="@{/main/layuimini/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/main/layuimini/css/public.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="searchForm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">演出计划名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">日期范围</label>
                            <div class="layui-inline" id="Date">
                                <div class="layui-input-inline">
                                    <input type="text" id="startDate" name="startDate" class="layui-input"
                                           placeholder="开始日期">
                                </div>
                                <div class="layui-form-mid">-</div>
                                <div class="layui-input-inline">
                                    <input type="text" id="endDate" name="endDate" class="layui-input"
                                           placeholder="结束日期">
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-block">
                                <input type="radio" name="status" value="已上线" title="已上线">
                                <input type="radio" name="status" value="待上线" title="待上线">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="search" type="submit" class="layui-btn layui-btn-radius layui-btn-normal"
                                    lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon layui-icon"></i> 搜 索
                            </button>
                            <button id="reset-form" type="reset" class="layui-btn layui-btn-radius layui-btn-primary">
                                <i class="layui-icon layui-icon-fonts-clear"></i> 清 空
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
                <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="online">上线计划</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="offline">下线计划</button>

            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">修改</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="online">上线计划</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-delete" lay-event="offline">下线计划</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script th:src=" @{/main/js/axios.min.js}"></script>
<script type="text/javascript" th:inline="none">
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg); //匹配目标参数
        if (r != null) {
            return decodeURIComponent(r[2]);
//这里为什么是从第三个字符解析呢？不知道这样理解对不对，因为路径后面的参数形式为参数名=参数值，而第一个字符为参数名，第二个为=，第三个就为参数值了。。。因为下面调用的时候得出的就是参数值
        }
        return null;//返回参数值
    }

    layui.use('laydate', function () {
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#Date'
            , range: ['#startDate', '#endDate']
        });
    });

    //判断选中的是否含有已上线的演出计划
    function beOnline(jsonArr) {
        for (let i = 0; i < jsonArr.length; i++) {
            if (jsonArr[i].status === '已上线'){
                return true;
            }
            return false;
        }
    }

    function refresh() {
        $('#reset-form').click();
        $('#search').click();
    }

    layui.use(['form', 'table', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            table = layui.table;
        table.render({
            elem: '#currentTableId',
            url: '/plan/getAllPlan?id=' + getUrlParam('id'),
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '电影列表',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }]
            ,
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 195, title: '编号', sort: true},
                {field: 'pName', width: 222, title: '演出计划名称'},
                {field: 'hName', width: 222, title: '演出位置'},
                {field: 'mName', width: 222, title: '电影名称'},
                {field: 'price', width: 150, title: '价格', sort: true},
                {field: 'status', width: 150, title: '状态', sort: true},
                {field: 'startDate', width: 200, title: '开始日期', sort: true,templet: function(d){
                        return dayjs(d.startDate).format('YYYY年MM月DD日 HH时MM分')}},
                {field: 'endDate', width: 200, title: '结束日期', sort: true,templet: function(d){
                        return dayjs(d.endDate).format('YYYY年MM月DD日 HH时MM分')}},
                {title: '操作', minWidth: 280,fixed:'right', toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 15, 20, 25, 50, 100],
            limit: 5,
            page: true,
            skin: 'line',
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1  //重新从第1页开始
                }
                , where: {
                    startDate: data.field.startDate,
                    endDate: data.field.endDate,
                    name: data.field.name,
                    status:data.field.status
                }
            });
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加演出演出计划',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/plan/add?mID=' + getUrlParam("id"),
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data.length == 0) {
                    layer.alert("您没有选中任何数据", {icon: 2, anim: 6, title: "警告"});
                    return false;
                }
                layer.confirm('真的删除行么', function () {
                    var Arrid = new Array();
                    for (var i = 0; i < data.length; i++) {
                        Arrid.push(data[i].id);
                    }
                    var ids = Arrid.join(",");
                    var url = '/plan/delete/' + ids;
                    axios.post(
                        url,
                    ).then(function (data) {
                        layer.msg(data, {
                            icon: 1,
                            title: "操作结果"
                        })
                        refresh();
                    })
                })
            }
            else if (obj.event === 'offline') {  // 监听下线操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data.length == 0) {
                    layer.alert("您没有选中任何数据", {icon: 2, anim: 6, title: "警告"});
                    return false;
                }
                if (!beOnline(data)){
                    layer.alert("您选中的演出计划含有已下线的演出计划", {icon: 2, anim: 6, title: "警告"});
                    return false;
                }
                console.log(data);
                layer.confirm('真的下线行么', function () {
                    axios.post('/ticket/deleteTicket',data).then(function (response) {
                        let data=response.data.data
                        layer.msg(data, {
                            icon: 1,
                            title: "操作结果"
                        })
                        refresh();
                    })
                })
            } else if (obj.event === 'online') {
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data.length == 0) {
                    layer.alert("您没有选中任何数据", {icon: 2, anim: 6, title: "警告"});
                    return false;
                }
                if (beOnline(data)){
                    layer.alert("您选中的演出计划含有已上线的演出计划", {icon: 2, anim: 6, title: "警告"});
                    return false;
                }
                axios.post('/ticket/insertTicket',data).then(function (response) {
                    let data=response.data.data
                    layer.msg(data, {
                        icon: 1,
                        title: "操作结果"
                    })
                    refresh();
                })
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                console.log(data.hname);
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/movie/getPlanById/' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                var data = obj.data;
                layer.confirm("您确定要删除" + data.pName + "行么", function () {
                    var id = data.id;
                    var url = '/plan/delete/'+ id;
                    $.post(url, function (data) {
                        layer.msg(data, {
                            icon: 1,
                            title: "操作结果"
                        })
                        refresh();
                    })
                });
            }
            else if (obj.event === 'online') {
                let jsonArr=[];
                jsonArr.push(data);
                if (beOnline(jsonArr)){
                    layer.alert("您选中的演出计划已上线", {icon: 2, anim: 6, title: "警告"});
                    return false;
                }
                axios.post('/ticket/insertTicket',jsonArr).then(function (response) {
                    let data=response.data.data
                    layer.msg(data, {
                        icon: 1,
                        title: "操作结果"
                    })
                    refresh();
                })
            }
            else if (obj.event === 'offline') {  // 监听下线操作
                let jsonArr=[];
                jsonArr.push(data);
                if (!beOnline(jsonArr)){
                    layer.alert("您选中的演出计划已下线", {icon: 2, anim: 6, title: "警告"});
                    return false;
                }
                layer.confirm("您确定要下线" + data.pName + "行么", function () {
                    axios.post('/ticket/deleteTicket',jsonArr).then(function (response) {
                        let data=response.data.data
                        layer.msg(data, {
                            icon: 1,
                            title: "操作结果"
                        })
                        refresh();
                    })
                })
            }
        });
    });
</script>
</body>
</html>